﻿using System;
using System.Collections.Generic;
using System.Text;
using log4net.Core;
using agsXMPP;
using agsXMPP.protocol.client;

namespace log4net.Extensions.Appender.XMPP
{
    public class XMPPDirectMessageAppender:XMPPAppender
    {
        public string Users { get; set; }

        public override void ActivateOptions()
        {
            base.ActivateOptions();

            if(!string.IsNullOrEmpty(this.Users))
                _UserList = System.Text.RegularExpressions.Regex.Split(this.Users, ";");

            for (int i = 0; i < _UserList.Length; i++)
            {
                if (_UserList[i].Contains("@"))
                    continue;

                _UserList[i] += "@" + this.Server;
            }

        }

        private string[] _UserList;

        protected override void Append(LoggingEvent loggingEvent)
        {
            if (null == _UserList || _UserList.Length == 0)
                return;

            string renderedMessage = base.RenderLoggingEvent(loggingEvent).Trim();

            foreach (string user in _UserList)
            {
                Message message = new Message();
                message.Type = MessageType.normal;
                message.Body = renderedMessage;
                message.To = new Jid(user);
                QueueMessage(message);
            }
        }
    }
}
